package com.github.willisaway.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CheckObject {
    public static String checkString(Object obj) {
        if (obj != null) {
            return obj.toString().equals("") ? null : obj.toString();
        }
        return null;
    }

    public static Long checkLongByObject(Object obj) {
        if (obj != null) {
            String str = checkString(obj);
            if (str != null)
                return Long.parseLong(str);
        }
        return (long) -1;
    }

    public static Integer checkIntegerByObject(Object obj) {
        if (obj != null) {
            String str = checkString(obj);
            if (str != null)
                return Integer.parseInt(str);
        }
        return null;
    }

    public static Date checkDateByObject(Object obj) throws ParseException {
        if (obj != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(checkString(obj));
        }
        return null;
    }
}
